// pages/lpl/index.js
const CONFIG = require('../../config.js');
let loginInfo = null;
Page({
	data: {
		hostUrl: CONFIG.url,
		navBgColor: '-webkit-gradient(linear, 0% 0%, 0% 100%,from(#1b64e5), to(#488dea))',
		loginInfo: null,
		formData: {},
		rules: [],
		showOneButtonDialog: false,
		oneButton: [{text: '确定'}],
		userData: '',
		avatar: '',
		isTrue: '0'
	},

	// 获取用户基本信息
	getUserInfoFn (){
		wx.request({
			url: CONFIG.host + "/system/user/getUserInfo",
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				if(res.data.code === 'success'){
					let datas = res.data.data;
					this.setData({
						userData: datas,
						avatar: datas.avatar ? 'https://wx.huidailang.cn/prod-api' + datas.avatar : ''
					})
			  	}
			},fail: function (error) {
			  	console.log(error);
			}
		})
	},
	
	// 解绑手机号
	untieFn(){
		wx.showToast({title: '加载中', icon: 'loading',mask:true, duration: 10000});
		if(this.data.loginInfo.openid === undefined) {
			wx.redirectTo ({
				url: '/pages/login/login'
			})
		}
		wx.request({
			url: CONFIG.host + "/system/weiXinLoginController/removeWeiXinBinding?openid="+ this.data.loginInfo.openid,
			method: "POST",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				wx.hideToast();
				if(res.data.code === 'success'){
					wx.redirectTo ({
						url: '/pages/login/login'
					})
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},
	
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		loginInfo = wx.getStorageSync('loginInfo') || '';
		this.setData({
			isTrue: wx.getStorageSync('showUser')
		})
	},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
			if(loginInfo){
				this.setData({
					loginInfo : JSON.parse(loginInfo)
				})
				this.getUserInfoFn();
			}else {
				wx.redirectTo ({
					url: '/pages/login/login'
				})
			}
    },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})